﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Campus.Main.Basic.Settings;
using Campus.Report.ReportForm;
using Microsoft.Reporting.WinForms;
using dbcntn;

namespace Campus.Main.Basic.BallSystem
{
    public partial class BallSystem : DefaultForm
    {
        private DataGridViewColumn aColumn1 = new DataGridViewColumn();
        private int polId;
        private int res;
        private int yedek;
        DataTable dTable = new DataTable("Puanlama");
        public string QuerySelect { get; set; }
        public BallSystem()
        {
            InitializeComponent();
            aToDb = new ConnectionToDb(Helper.ConnectionString);
            res = 1;
            yedek = 0;
        }

        #region Methodds

        public override void Initialize()
        {
            radioButton5.Checked=radioButton3.Checked = radioButton4.Checked = radioButton7.Checked = false;
            dataGridView1.DataSource = null;
            btn_erkek_yurt.Visible = false;
            btn_erkek_yurt.Enabled = true;
            btn_erkek_liste.Visible = false;
            btn_kiz_yurt.Visible = false;
            btn_kiz_yurt.Enabled = true;
            btn_kiz_liste.Visible = false;
            btn_y_e.Visible = false;
            btn_y_k.Visible = false;
            btn_kayit.Visible = false;
            btn_y_list_k.Visible = btn_y_list_e.Visible = false;
        }

        #endregion

        #region DataGridView

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1)
            {
                return;
            }
        }

        #endregion

        #region TextBox

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }

        #endregion

        #region RadioButton

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                polId = 1;
                btn_erkek_yurt.Visible = true;
                btn_erkek_liste.Visible = true;
                btn_kiz_yurt.Visible = false;
                btn_kiz_liste.Visible = false;
                btn_y_list_e.Visible = btn_y_e.Visible = false;
                btn_y_list_k.Visible = btn_y_k.Visible = false;
                string temp = aToDb.SelectField("select max(ball) from onmaintable where pol_id=1 and res=0");
                txt_max.Text = temp;
                txt_max.Enabled = true;
                txt_min.Enabled = true;
                txt_count.Enabled = true;
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked)
            {
                polId = 2;
                btn_erkek_yurt.Visible = false;
                btn_erkek_liste.Visible = false;
                btn_kiz_yurt.Visible = true;
                btn_kiz_liste.Visible = true;
                btn_y_list_e.Visible=btn_y_e.Visible = false;
                btn_y_list_k.Visible = btn_y_k.Visible = false;
                string temp = aToDb.SelectField("select max(ball) from onmaintable where pol_id=2 and res=0");
                txt_max.Text = temp;
                txt_max.Enabled = true;
                txt_min.Enabled = true;
                txt_count.Enabled = true;
            }
        }





        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton5.Checked)
            {
                //nationId = 8;
            }
        }

        #endregion

        #region Button

        private void button1_Click(object sender, EventArgs e)
        {
            
            if (txt_max.Text.Length > 0 && txt_min.Text.Length > 0 && txt_count.Text.Length > 0)
            {
                string query = String.Empty;
                if (radioButton1.Checked)
                {
                    query = "select a.no,a.student_id,a.name,b.description as fakulty,c.description as bolum1,d.description AS sinif,a.ball,a.res from onmaintable a,fakulty b,bolum c,cource d " +
                            "where c.fakulty_id=b.id and a.bolum_id=c.id and d.id=a.cource_id and a.cource_id<>6 and a.res=0 and a.pol_id=" + polId + " and a.ball<=" + txt_max.Text +
                    " and a.ball>=" + txt_min.Text + " order by a.ball desc,a.no limit 0," + txt_count.Text;
                }
                if (radioButton2.Checked)
                {
                    query = "select a.no,a.student_id,a.name,b.description as fakulty,c.description as bolum1,d.description AS sinif,a.ball,a.res from onmaintable a,fakulty b,bolum c,cource d " +
                            "where c.fakulty_id=b.id and a.bolum_id=c.id and d.id=a.cource_id and a.cource_id<>6 and a.yedek=0 and res=0 and a.pol_id=" + polId + " and a.ball<=" + txt_max.Text +
                    " and a.ball>=" + txt_min.Text + " order by a.ball desc,a.no limit 0," + txt_count.Text;
                }
                QuerySelect = query;
                aToDb.AddItemDBGridView(dataGridView1, QuerySelect);
                dTable = aToDb.GetDataTable(QuerySelect);
                // dataGridView1.Columns[1].Visible = false;
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = i + 1;
                }
                btn_kayit.Visible = true;
                btn_erkek_yurt.Enabled = false;
                btn_kiz_yurt.Enabled = false;
                Width = dataGridView1.Width+15;
            }
            else
            {
                MessageBox.Show("Талаларды толтургула");
            }
        }

        private void btn_kayit_Click(object sender, EventArgs e)
        {
            List<int> col = new List<int>();
            btn_erkek_yurt.Enabled = false;
            btn_kiz_yurt.Enabled = false;
            if (MessageBox.Show("Siralamani onaylamak istiyormusunuz???", "Dikkat", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    if (Convert.ToBoolean(dataGridView1[8, i].Value) != true)
                    {
                        col.Add(i);
                    }
                }
                col.Reverse();
                foreach (var i in col)
                {
                    dTable.Rows[i].Delete();
                }
                foreach (var i in col)
                {
                    aToDb.UpdateMainTable("res","0","student_id",Convert.ToString(dataGridView1[2,i].Value));
                    aToDb.UpdateMainTable("yedek", "0", "student_id", Convert.ToString(dataGridView1[2, i].Value));
                }
                dTable.AcceptChanges();
                dataGridView1.DataSource = dTable;
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = i + 1;
                    dataGridView1.Rows[i].Cells[8].Value = true;
                }
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    aToDb.UpdateMainTable("res", res, Convert.ToString(dataGridView1[2, i].Value));
                    aToDb.UpdateMainTable("yedek", yedek, Convert.ToString(dataGridView1[2, i].Value));
                    //aToDb.InsertIntoKMainTable(Convert.ToString(dataGridView1[1, i].Value));
                }
                if (MessageBox.Show("Liste almak istiyormisinuz???", "Duyuru", MessageBoxButtons.YesNo) ==
                    DialogResult.Yes)
                {
                   Print();
                    /*if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        var aReport = new KesinKayitReport(saveFileDialog1);
                        aReport.Printreport(dataGridView1, "Kesin Kayit Listesi", dataGridView1.RowCount, 1,2, 3, 4, 5,6);
                        Process.Start(aReport.SaveDialog.FileName);
                    }*/
                }
            }
        }

        void Print()
        {
            string donem = aToDb.SelectField("select donem from onmaintable where no=1 and pol_id=1");
            string pol = aToDb.SelectField("select description from pol where id=" + polId);
            string polkg = pol == "Erkek" ? "Эркек" : "Кыз";
            string test = String.Empty;
            string testkg = String.Empty;
            if (res == 1 && yedek == 0)
            {
                test = "Kesin Kayit Hakki Kazan";
                testkg = "кирген";
            }
            else if (res == 0 && yedek == 1)
            {
                test = "Yedekteki";
                testkg = "кезекте турган";
            }
            var par = new List<ReportParameter>();
            par.Add(new ReportParameter("donem", donem));
            par.Add(new ReportParameter("pol", pol));
            par.Add(new ReportParameter("polkg", polkg));
            par.Add(new ReportParameter("test", test));
            par.Add(new ReportParameter("testkg", testkg));
            ReportForm f = new ReportForm();
            f.reportViewer1.ProcessingMode = ProcessingMode.Local;
            f.reportViewer1.LocalReport.ReportPath = "ReportTemplate/PuanlamaReport.rdlc";
            f.reportViewer1.LocalReport.DataSources.Clear();
            f.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dataGridView1.DataSource));
            f.reportViewer1.LocalReport.SetParameters(par);
            f.ShowDialog();
        }

        private void btn_erkek_liste_Click(object sender, EventArgs e)
        {
            string query = String.Empty;
            btn_kayit.Visible = true;
            if (radioButton1.Checked)
            {
                query =
                    "select a.no,a.student_id,a.name,b.description as fakulty,c.description as bolum1,d.description as sinif,a.regdate,a.ball,a.res from onmaintable a,fakulty b,bolum c,cource d " +
                    " where c.fakulty_id=b.id and a.bolum_id=c.id and a.cource_id=d.id and res=1 and pol_id=" +
                    polId + " order by ball desc,no";
            }
            if (radioButton2.Checked)
            {
                query =
                    "select a.no,a.student_id,a.name,b.description as fakulty,c.description as bolum1,d.description as sinif,a.regdate,a.ball,a.res from onmaintable a,fakulty b,bolum c,cource d where " +
                    "c.fakulty_id=b.id and a.bolum_id=c.id and d.id=a.cource_id and yedek=1 and pol_id=" +
                    polId + " order by ball desc,no";
            }
            QuerySelect = query;
            aToDb.AddItemDBGridView(dataGridView1, QuerySelect);
            dTable = aToDb.GetDataTable(QuerySelect);
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                dataGridView1.Rows[i].Cells[0].Value = i + 1;
                dataGridView1.Rows[i].Cells[7].ReadOnly = false;
            }
            if (MessageBox.Show("Liste almak istiyormusunuz???","Deikkat",MessageBoxButtons.YesNo)==DialogResult.Yes)
            {
                Print();
            }
        }

        #endregion

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1)
            {
                return;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                Initialize();
                res = 1;
                yedek = 0;
            }
            
        }


        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                Initialize();
                yedek = 1;
                res = 0;
            }
        }

        private void BallSystem_Load(object sender, EventArgs e)
        {
           // btn_kayit.Enabled = false;
            Initialize();
        }

        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton7.Checked)
            {
                polId = 1;
                btn_erkek_yurt.Visible = false;
                btn_erkek_liste.Visible = false;
                btn_kiz_yurt.Visible = false;
                btn_kiz_liste.Visible = false;
                btn_y_list_e.Visible = btn_y_e.Visible = true;
                btn_y_list_k.Visible = btn_y_k.Visible = false;
                string temp = aToDb.SelectField("select max(ball) from onmaintable where pol_id=1 and res=0 and cource_id=6");
                txt_max.Text = temp;
                txt_max.Enabled = true;
                txt_min.Enabled = true;
                txt_count.Enabled = true;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string query = String.Empty;
            if (txt_count.Text.Length>0)
            {
                if (radioButton1.Checked)
                {
                    query="select a.no,a.student_id,a.name,b.description as fakulty,c.description as bolum1,a.regdate,a.ball,a.res from onmaintable a,fakulty b,bolum c " +
                            "where c.fakulty_id=b.id and a.bolum_id=c.id and res=0 and a.cource_id=6 and a.pol_id=" + polId + " and a.ball<=" + txt_max.Text +
                    " and a.ball>=" + txt_min.Text + " order by ball desc,no limit 0," + txt_count.Text;
                }
                if (radioButton2.Checked)
                {
                    query = "select a.no,a.student_id,a.name,b.description as fakulty,c.description as bolum1,a.regdate,a.ball,a.res from onmaintable a,fakulty b,bolum c " +
                            "where c.fakulty_id=b.id and a.bolum_id=c.id and yedek=0 and res=0 and a.cource_id=6 and pol_id=" + polId + " and ball<=" + txt_max.Text +
                    " and ball>=" + txt_min.Text + " order by ball desc,no limit 0," + txt_count.Text;
                }
                txt_max.Enabled = false;
                txt_min.Enabled = true;
                txt_count.Enabled = true;
                btn_kayit.Visible = true;
                QuerySelect = query;
                aToDb.AddItemDBGridView(dataGridView1, QuerySelect);
                dTable = aToDb.GetDataTable(QuerySelect);
                // dataGridView1.Columns[1].Visible = false;
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = i + 1;
                }
                btn_kayit.Visible = true;
                btn_erkek_yurt.Enabled = false;
                btn_kiz_yurt.Enabled = false;
                Width = dataGridView1.Width + 15;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
           
        }

        private void radioButton5_CheckedChanged_1(object sender, EventArgs e)
        {
            if (radioButton5.Checked)
            {
                polId = 2;
                btn_erkek_yurt.Visible = false;
                btn_erkek_liste.Visible = false;
                btn_kiz_yurt.Visible = false;
                btn_kiz_liste.Visible = false;
                btn_y_list_e.Visible = btn_y_e.Visible = false;
                btn_y_list_k.Visible = btn_y_k.Visible = true;
                string temp = aToDb.SelectField("select max(ball) from onmaintable where pol_id=2 and res=0 and cource_id=6");
                txt_max.Text = temp;
                txt_max.Enabled = true;
                txt_min.Enabled = true;
                txt_count.Enabled = true;
            }
        }

        private void btn_y_list_e_Click(object sender, EventArgs e)
        {
            string query = String.Empty;
            btn_kayit.Visible = true;
            if (radioButton1.Checked)
            {
                query =
                    "select a.no,a.student_id,a.name,b.description as fakulty,c.description as bolum1,d.description as sinif,a.regdate,a.ball,a.res from onmaintable a,fakulty b,bolum c,cource d " +
                    "where c.fakulty_id=b.id and a.bolum_id=c.id and d.id=a.cource_id and res=1 and pol_id=" +
                    polId + " and a.cource_id=6 order by ball desc,no";
            }
            if (radioButton2.Checked)
            {
                query =
                    "select a.no,a.student_id,a.name,b.description as fakulty,c.description as bolum1,d.description as sinif,a.regdate,a.ball,a.res from onmaintable a,fakulty b,bolum c,cource d where " +
                    "c.fakulty_id=b.id and a.bolum_id=c.id and d.id=a.cource_id and yedek=1 and pol_id=" +
                    polId + " and a.cource_id=6 order by ball desc,no";
            }
            QuerySelect = query;
            aToDb.AddItemDBGridView(dataGridView1, QuerySelect);
            dTable = aToDb.GetDataTable(QuerySelect);
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                dataGridView1.Rows[i].Cells[0].Value = i + 1;
                dataGridView1.Rows[i].Cells[7].ReadOnly = false;
            }
            if (MessageBox.Show("Liste almak istiyormusunuz???", "Deikkat", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Print();
            }
        }

    }
}

